package com.ljc.controller;

import com.google.common.base.Strings;
import com.ljc.model.Admin;
import com.ljc.model.User;
import com.ljc.service.IUserService;
import com.ljc.utils.MapControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;


    @RequestMapping("/tologin")
    public String tologin(){
        System.out.println("tostringsucc");
        return "login";
    }

    @RequestMapping("/first")
    public String tofirst(HttpSession session){

        return "first";
    }
    @RequestMapping("/toregister")
    public String toregister(){
        return "register";
    }

    @RequestMapping("/touser")
    public String touser(){
        return "user";
    }





//    @RequestMapping("/login")
//    public ModelAndView login(@RequestParam("username") String username,
//                              @RequestParam("password") String password,Model model) throws Exception {
//        ModelAndView mv = new ModelAndView();
//        User loginuser = new User();
//        System.out.println(username);
//        System.out.println(password);
//        loginuser.setUsername(username);
//        loginuser.setPassword(password);
//        User user = userService.userLogin(loginuser);
//        if(user!=null){
//
//            mv.addObject("user", user);
//            mv.setViewName("first");
//            return mv;
//        }
//        else {
//            model.addAttribute("error","账号或密码错误");
//            mv.setViewName("login");
//            return mv;
//        }
//    }


    @PostMapping("/login")
    @ResponseBody
    public Map<String,Object> login(@RequestBody Map<String,Object> map, HttpServletRequest request){

        String username=map.get("username")+"";
        String password=map.get("password")+"";
        if(Strings.isNullOrEmpty(username)||Strings.isNullOrEmpty(password))
        {
            return MapControl.getInstance().error("用户名或密码不能为空").getMap();
        }
       User user = userService.login(username,password);
        if (user!=null){
            HttpSession session = request.getSession();
            session.setAttribute("user",user);

            return MapControl.getInstance().success("登陆成功").getMap();
        }else
        {
            return MapControl.getInstance().error("用户名或密码cw").getMap();

        }

    }



    @RequestMapping("/register")
    public String  register(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            @RequestParam("steamid") int steamid){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setSteamid(steamid);
        userService.register(user);
        System.out.println("registersucc");
        System.out.println(user);
        return "login";
    }







    }

